﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;

namespace Journeo_backend.Controllers
{
    [Route("api/user")]
    [ApiController]
    [Authorize]
    public class UserController : ControllerBase
    {
        private readonly IUserService _userService;

        public UserController(IUserService userService)
        {
            _userService = userService;
        }

        [HttpPost("register")]
        [SwaggerOperation(Summary = "用户注册", Description = "使用邮箱和密码进行注册")]
        [SwaggerResponse(200, "注册成功", typeof(Response<string>))]
        [SwaggerResponse(400, "注册失败，输入无效")]
        [AllowAnonymous]
        public async Task<IActionResult> Register(UserRegisterDTO userRegisterDTO)
        {
            var response = await _userService.RegisterAsync(userRegisterDTO);
            return Ok(response);
        }

        [HttpPost("login")]
        [SwaggerOperation(Summary = "用户登录", Description = "使用邮箱和密码进行登录")]
        [SwaggerResponse(200, "登录成功", typeof(Response<UserLoginVO>))]
        [SwaggerResponse(400, "登录失败，输入无效")]
        [AllowAnonymous]
        public async Task<IActionResult> Login(UserLoginDTO loginDTO)
        {
            var response = await _userService.LoginAsync(loginDTO);
            return Ok(response);
        }

        [HttpPost("forgetPassword")]
        [SwaggerOperation(Summary = "忘记密码", Description = "通过邮箱重置密码")]
        [SwaggerResponse(200, "重置成功", typeof(Response<string>))]
        [SwaggerResponse(400, "重置失败，邮箱无效")]
        [AllowAnonymous]
        public async Task<IActionResult> ForgetPassword(UserForgetPasswordDTO forgetPasswordDTO)
        {
            var response = await _userService.ForgetPasswordAsync(forgetPasswordDTO);
            return Ok(response);
        }

        [HttpGet("{userId}")]
        [SwaggerOperation(Summary = "获取用户信息", Description = "通过用户ID获取用户信息")]
        [SwaggerResponse(200, "获取成功", typeof(Response<UserInfoVO>))]
        [SwaggerResponse(400, "获取失败，用户ID无效")]
        public async Task<IActionResult> GetUser(long userId)
        {
            var response = await _userService.GetUserAsync(userId);
            return Ok(response);
        }

        [HttpPut("update")]
        [SwaggerOperation(Summary = "更新用户信息", Description = "通过用户ID更新用户信息")]
        [SwaggerResponse(200, "更新成功", typeof(Response<string>))]
        [SwaggerResponse(400, "更新失败，用户ID无效")]
        public async Task<IActionResult> UpdateUser(UserUpdateDTO userUpdateDTO)
        {
            var response = await _userService.UpdateUserAsync(userUpdateDTO);
            return Ok(response);
        }

        [HttpPost("avatar/upload/base64")]
        [SwaggerOperation(Summary = "上传用户头像base64", Description = "默认头像Url为“default”，不为默认时替换")]
        [SwaggerResponse(200, "上传成功", typeof(Response<string>))]
        [SwaggerResponse(400, "上传失败，用户ID无效")]
        public async Task<IActionResult> UploadAvatar(long userId, string base64Image)
        {
            var response = await _userService.UploadAvatarAsync(userId, base64Image);
            return Ok(response);
        }

        [HttpPost("avatar/upload/file")]
        [SwaggerOperation(Summary = "上传用户头像file", Description = "默认头像Url为“default”，不为默认时替换")]
        [SwaggerResponse(200, "上传成功", typeof(Response<string>))]
        [SwaggerResponse(400, "上传失败，用户ID无效")]
        public async Task<IActionResult> UploadAvatar(long userId, IFormFile file)
        {
            var response = await _userService.UploadAvatarAsync(userId, file);
            return Ok(response);
        }

        [HttpGet("avatar/{userId}")]
        [SwaggerOperation(Summary = "ID下载用户头像", Description = "通过用户Id下载用户头像")]
        [SwaggerResponse(200, "下载成功", typeof(Response<string>))]
        [SwaggerResponse(400, "下载失败，头像URL无效")]
        public async Task<IActionResult> GetAvatar(long userId)
        {
            var response = await _userService.GetAvatarAsync(userId);
            return Ok(response);
        }

        [HttpPost("sendcode")]
        [SwaggerOperation(Summary = "发送验证码", Description = "发送验证码到邮箱")]
        [SwaggerResponse(200, "发送成功", typeof(Response<string>))]
        [SwaggerResponse(400, "发送失败，邮箱无效")]
        [AllowAnonymous]
        public async Task<IActionResult> SendVerificationCode([FromBody] SendCodeRequest request)
        {
            var result = await _userService.SendVerificationCodeAsync(request.Email, request.Type);
            return Ok(result);
        }
    }
}
